package com.mifan.security;/*
 * @Author Milan
 * @Date 2024/9/13 17:38
 */



import com.mifan.entity.Resources;
import com.mifan.entity.Role;
import com.mifan.entity.User;
import com.mifan.mapper.ResourceMapper;
import com.mifan.mapper.RoleMapper;
import com.mifan.service.IResourceService;
import com.mifan.service.IUserService;

import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Component
public class CustomUserDetailsService implements UserDetailsService {

    @Resource
    private IResourceService resourceService;

    @Resource
    private IUserService userService;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("执行了 loadUserByUsername ");
        // TODO 查找用户
        User user = userService.getUserByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户名不存在");
        }
        System.out.println("是否差User数据" + user.getPassword());

        // TODO 查找角色
        List<String> resources = resourceService.getResourcesByUserUsername(username);

        return new CustomUserDetails(
                user.getUsername(),
                user.getPassword(),
                user.getStatus(),
                resources
        );
    }
}
